package com.lanou3g.mynewstest.module.news.pictures;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
import com.lanou3g.mynewstest.R;
import com.lanou3g.mynewstest.base.MyBaseFragment;
import com.lanou3g.mynewstest.beans.NewsPicturesBeans;
import com.lanou3g.mynewstest.tools.MyRequestQueue;
import com.lanou3g.mynewstest.tools.StringRequestUtf;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by dllo on 16/1/8.
 */
public class PicturesFragment extends MyBaseFragment {

    private ListView listView;
    private PicturesAdapter adapter;

    private final String url="http://c.m.163.com/photo/api/list/0096/4GJ60096.json";
    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        View view=inflater.inflate(R.layout.fragment_news_pictures,null);
        listView= (ListView) view.findViewById(R.id.pictures_lv);
        getBeans(url);


        return view;
    }
    private void getBeans(String url) {
        StringRequestUtf request = new StringRequestUtf(url,
                new Response.Listener<String>() {
                    @Override
                    public void onResponse(String response) {

                        Gson gson = new Gson();

                        List<NewsPicturesBeans> beans;
                        Type type =  new TypeToken<ArrayList<NewsPicturesBeans>>() {}.getType();
                        beans=gson.fromJson(response,type);

                        adapter=new PicturesAdapter(context,beans);
                        listView.setAdapter(adapter);

                    }
                },
                new Response.ErrorListener() {
                    @Override
                    public void onErrorResponse(VolleyError error) {

                    }
                });

        MyRequestQueue.addRequest(request);
    }
}
